package com.jxb.test;

import java.util.Properties;
import java.util.concurrent.Executor;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/10/11 20:40
 **/
public class ConfigServerDemo {

    public static void main(String[] args) throws NacosException {
        String serverAddr = "localhost";
        String dataId = "nacos-config-demo.yaml";
        String group = "DEFAULT_GROUP";
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);

        //获取配置服务
        ConfigService configService = NacosFactory.createConfigService(properties);
        //获取配置
        String config = configService.getConfig(dataId, group, 5000);
        System.out.println(config);
        //注册监听器
        configService.addListener(dataId, group, new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String configInfo) {
                System.out.println("===recieve:" + configInfo);
            }
        });

        //发布配置
        boolean isPublishOk  = configService.publishConfig(dataId, group, "content");
        System.out.println(isPublishOk );

        //移除配置
        boolean b = configService.removeConfig(dataId, group);
        System.out.println(b);
    }

}
